package com.example.italkapp2.controller.activity;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;

import com.example.italkapp2.R;
import com.example.italkapp2.controller.fragment.ChatFragment;
import com.example.italkapp2.controller.fragment.ContactListFragment;
import com.example.italkapp2.controller.fragment.SettingFragment;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.widget.EaseTitleBar;

public class MainActivity extends FragmentActivity {

    private RadioGroup rg_main;
    private ChatFragment chatFragment;
    private ContactListFragment contactListFragment;
    private SettingFragment settingFragment;
    private EaseTitleBar tb_main_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        //以下两个方法是为了保证进入主页面后本地会话和群组都 load 完毕。
        //另外如果之前登录过，APP 长期在后台再进的时候也可能会导致加载到内存的群组和会话为空，可以在主页面的 oncreate 里也加上这两句代码
        EMClient.getInstance().groupManager().loadAllGroups();
        EMClient.getInstance().chatManager().loadAllConversations();

        intiViews();
        initData();
        initListener();
    }


    private void intiViews(){
        tb_main_title=findViewById(R.id.tb_main_title);
        rg_main=findViewById(R.id.rg_main);
    }

    private void initData(){
        //创建3个fragment对象
        chatFragment = new ChatFragment();
        contactListFragment = new ContactListFragment();
        settingFragment = new SettingFragment();
    }

    private void initListener(){
        //radioGroup的选择事件
        rg_main.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                Fragment fragment=null;
                String titleName="Fragment";

                switch (checkedId){
                    case R.id.rb_main_chat:
                        fragment=chatFragment;
                        titleName="消息";
                        break;
                    case R.id.rb_main_contact_list:
                        fragment=contactListFragment;
                        titleName="联系人";
                        break;
                    case R.id.rb_main_setting:
                        fragment=settingFragment;
                        titleName="我的";
                        break;
                }

                //实现fragment切换的方方
                switchFragment(fragment,titleName);
            }
        });

        //默认选择消息列表页面
        rg_main.check(R.id.rb_main_chat);

    }

    //切换fragment
    private void switchFragment(Fragment fragment,String titleName) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fl_main, fragment);
        ft.show(fragment);
        ft.commit();

        //设置标题栏
        tb_main_title.setTitle(titleName);

        if (fragment==contactListFragment){
            //获取加号按钮点击事件
            //tb_main_title=findViewById(R.id.tb_main_title);
            tb_main_title.setRightLayoutVisibility(1);

            //按钮点击事件处理
            tb_main_title.setRightLayoutClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, AddContactActivity.class);
                    startActivity(intent);
                }
            });
        }else {
            tb_main_title.setRightLayoutVisibility(-1);
        }

    }

}